﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BigDataFramework.Helpers;
using System.Web;
using BigData.DataContracts.Objects.Reddit;
using BigData.DataContracts.Objects.BigData;
using MongoDB.Bson;
using BigDataFramework.Library.Helpers;

namespace BigDataFramework.Library.Objects.DataAdapters.Reddit
{
    public class SubmittedAdapter : DataAdapters.DataAdapterBase<Link>
    {
        public string _user { get; set; }

        public SubmittedAdapter(string user)
        {
            this._user = user;
        }

        /// <summary>
        /// Returns a list of links submitted by the user.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        protected override List<Link> RetrieveDataFromAPIForUser(BigDataUser user, DateTime startDate, DateTime endDate)
        {

            RestSharp.RestRequest request = new RestSharp.RestRequest(RestSharp.Method.GET);
            request.Resource = string.Format("user/{0}/submitted/.json", this._user);

            return RedditAdapter.GetAll<Link>(startDate, endDate, request);

        }

        public override Event ConvertObjToEvent(Link obj)
        {
            // Create an event from the BigDataFourSquareCheckin details
            Event output = new Event()
            {
                Headline            = string.Format("Submitted '{0}'", obj.title),
                Text                = obj.url,
                AffiliatedUserIds   = obj.AssociatedUserIds.Select<string, ObjectId>(uid => ObjectId.Parse(uid)).ToList(),
                //DataProviderId    = ObjectId.Parse("Reddit"),
                StartDate           = DateTimeHelper.FromUnixTimeStamp(obj.created_utc),
                EndDate             = DateTimeHelper.FromUnixTimeStamp(obj.created_utc),
            };

            //If the url is just a link to an image then include that as the media
            if (obj.url.EndsWith(".jpg") || obj.url.EndsWith(".jpeg"))
            {
                output.Media = new EventMedia();
                output.Media.URL = obj.url;
            }
            else if (obj.media_embed != null)
            {
                output.Media = new EventMedia();
                //This is the embed information. I don't know if this will hand this
                output.Media.URL = obj.url;
            }

            return output;
        }
    }
}
